package com.wteam.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wteam.PoJo.DO.Goods;
import com.wteam.PoJo.DO.Params;
import com.wteam.PoJo.DO.Presale;
import com.wteam.PoJo.DTO.PageQuery;
import com.wteam.PoJo.VO.GoodsVO;
import com.wteam.PoJo.VO.ParamsVO;
import com.wteam.PoJo.VO.PresaleParamsVO;
import com.wteam.PoJo.VO.PresaleVO;
import com.wteam.mapper.PresaleMapper;
import com.wteam.Service.IPresaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wteam.until.Result;
import org.apache.lucene.search.MultiCollectorManager;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 预售表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-16
 */
@Service
public class PresaleServiceImpl extends ServiceImpl<PresaleMapper, Presale> implements IPresaleService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result<PresaleVO> selectpresalegood(PageQuery pageQuery) {

        String key = pageQuery.getPage() + "_" + pageQuery.getPageSize()
                + "_" + pageQuery.getSortBy() + "_" + pageQuery.getIsAsc();

        //查缓存
        List<PresaleVO> goodsVOS1 = (List<PresaleVO>) redisTemplate.opsForHash().get("PresaleVO", key);

        if (CollUtil.isNotEmpty(goodsVOS1)){
            return Result.success(goodsVOS1);
        }

        Page<Presale> presalegoodPage = pageQuery.toMpPage();
        Page<Presale> page = lambdaQuery().page(presalegoodPage);
        List<Presale> records = page.getRecords();
        if (CollUtil.isEmpty(records)) {
            return Result.success();
        }
        List<PresaleVO> goodsVOS = BeanUtil.copyToList(records, PresaleVO.class);

        //存缓存
        redisTemplate.opsForHash().put("PresaleVO", key,goodsVOS);

        return Result.success(goodsVOS);
    }

    @Override
    public Result<List<ParamsVO>> selectgoods(List<Long> ids) {

        List<Presale> list = lambdaQuery().in(Presale::getId, ids).list();
        if (CollUtil.isEmpty(list)) {
            return Result.success();
        }
        List<PresaleVO> presaleVOS = BeanUtil.copyToList(list, PresaleVO.class);
        //预售商品id
        List<Long> collect = presaleVOS.stream().map(PresaleVO::getId).collect(Collectors.toList());
        //查详情
        List<Params> list1 = Db.lambdaQuery(Params.class)
                .in(Params::getGoodsId, collect).eq(Params::getType, 2).list();
        //商品id和详情关系
        Map<Long, Params> collect1 = list1.stream().collect(Collectors.toMap(Params::getGoodsId, Function.identity()));
        List<PresaleParamsVO>presaleParamsVOS=new ArrayList<>(collect.size());

return null;
    }
}
